package JUC.bq;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class Test {
    public static void main(String[] args) throws InterruptedException {
        //Collection
        //list
        //set
        test1();
        test2();
        test3();
        test4();
    }

    /**
     * 抛出异常
     */
    public static void test1(){
        // 队列的大小：3
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(arrayBlockingQueue.add("a"));
        System.out.println(arrayBlockingQueue.add("b"));
        System.out.println(arrayBlockingQueue.add("c"));

        //查看队首元素
        System.out.println(arrayBlockingQueue.element());
        //IllegalStateException: Queue full
//      System.out.println(arrayBlockingQueue.add("d"));


        System.out.println("======================");


        //先进先出
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());

        //Exception in thread "main" java.util.NoSuchElementException
//        System.out.println(arrayBlockingQueue.remove());
    }

    public static void test2(){
        ArrayBlockingQueue<Object> arrayBlockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));

        //检测队首元素
        System.out.println(arrayBlockingQueue.peek());
        //返回boolean值，不抛出异常
//        System.out.println(arrayBlockingQueue.offer("d"));


        System.out.println("=========================");


        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        //返回null，不抛出异常
        System.out.println(arrayBlockingQueue.poll());

    }

    /**
     * 等待，阻塞（一直阻塞）
     */
    public static void test3() throws InterruptedException {
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);

        //加的过程一直阻塞，不会返回boolean值
        arrayBlockingQueue.put("a");
        arrayBlockingQueue.put("b");
        arrayBlockingQueue.put("c");
        //没有位置，一直等待
//        arrayBlockingQueue.put("d");


        System.out.println("==========================");


        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        //没有这个元素，一直等待
//        System.out.println(arrayBlockingQueue.take());

    }

    public static void test4() throws InterruptedException {
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);

        arrayBlockingQueue.offer("a");
        arrayBlockingQueue.offer("b");
        arrayBlockingQueue.offer("c");
        //假设要添加d,前面是满的，等待两秒，如果没有则退出不继续等待
        arrayBlockingQueue.offer("d",2,TimeUnit.SECONDS);


        System.out.println("===================");


        arrayBlockingQueue.poll();
        arrayBlockingQueue.poll();
        arrayBlockingQueue.poll();
        //先等待着拿两秒，超过两秒之后就退出
        arrayBlockingQueue.poll(2,TimeUnit.SECONDS);

    }

}
